import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test3 {
    public static void main(String[] args) {
//        permuteUnique(new int[]{1,1,2});
        int[] arr = {1,8,3,6,1,4};
        Arrays.sort(arr);
        System.out.println();
    }

    static List<List<Integer>> ret = new ArrayList<>();
    static List<Integer> path = new ArrayList<>();
    static boolean[] check;
    public static List<List<Integer>> permuteUnique(int[] nums) {
        int n = nums.length;
        check = new boolean[n];
        dfs(nums);
        return ret;
    }

    public static void dfs(int[] nums) {
        if(path.size() == nums.length) {
            ret.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < nums.length; i++) {
            if(check[i] == false) {
                int j = 0;
                for(; j < i; j++) {
                    if(check[j] && nums[j] == nums[i]) {
                        break; // 没有被选中但是此元素与前面被选中的某个元素相等
                    }
                }
                if(j == i) {
                    path.add(nums[i]);
                    check[i] = true;
                    dfs(nums);
                    path.remove(path.size() - 1);
                    check[i] = false;
                }
            }
        }
    }
}
